<?php

namespace App\Admin\Controllers;

use App\Article;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class ArticleController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('教师风采');
            $content->description('文章列表');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     *
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('教师风采');
            $content->description('文章编辑');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('教师风采');
            $content->description('新增文章');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Article::class, function (Grid $grid) {

            $grid->filter(function ($filter) {
                $filter->disableIdFilter();
                $filter->like('title', '文章标题');
                $filter->like('teacher', '教师名称');
            });
            $grid->disableExport();

            //$grid->id('ID')->sortable();
            //$grid->column('title', '文章标题');
            $grid->column('teacher', '教师姓名');
            $grid->age('教师年龄');
            $grid->education('学历');
            $grid->experience('经验');
            //$grid->column('category', '文章栏目名');

            $grid->updated_at('修改时间')->sortable();
        });
    }


    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Article::class, function (Form $form) {
            $form->display('id', 'ID');
            $form->hidden('category', '栏目')->value('教师风采');
            $form->hidden('title', '标题')->value('无题');

            $form->text('teacher', '教师姓名');
            $form->text('education', '学历');
            $form->number('experience', '经验（年）');
            $form->number('age', '教师年龄');
            $form->image('picture', '教师图片')->uniqueName();
            $form->textarea('sentence', '教师格言');
            $form->editor('content', '教师风采');
//			$form->display( 'created_at', 'Created At' );
//			$form->display( 'updated_at', 'Updated At' );
        });
    }
}
